package top.fengyulou.xiaofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import top.fengyulou.xiaofeng.common.MyUtils;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.domain.QuestionClassify;
import top.fengyulou.xiaofeng.dto.ClassifyDto;
import top.fengyulou.xiaofeng.dto.QuestionDto;
import top.fengyulou.xiaofeng.dto.QuestionOptionDto;
import top.fengyulou.xiaofeng.enums.QuestionTypeEnum;
import top.fengyulou.xiaofeng.mapper.QuestionClassifyMapper;
import top.fengyulou.xiaofeng.mapper.QuestionMapper;
import top.fengyulou.xiaofeng.mapper.QuestionOptionMapper;
import top.fengyulou.xiaofeng.mapper.my.MyQuestionMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName QuestionService
 * @Description TODO
 * @Author qwer
 * @Date 2021/8/22 1:54
 * @Version 1.0
 */
@Service
public class QuestionService {

    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private MyQuestionMapper myQuestionMapper;
    @Resource
    private QuestionOptionMapper questionOptionMapper;
    @Resource
    private QuestionClassifyMapper questionClassifyMapper;

    /**
     * 列表查询
     */
    public void list(QuestionDto questionDto) {
        //查询数据
        PageHelper.startPage(questionDto.getPageNumber(), questionDto.getPageSize());
        List<QuestionDto> questionList = questionMapper.list(questionDto);
        PageInfo<QuestionDto> pageInfo = new PageInfo<>(questionList);

        //查询试题选项
        if (!CollectionUtils.isEmpty(questionList)){
            QuestionOptionDto questionOptionDto = new QuestionOptionDto();
            for (QuestionDto question : questionList) {
                questionOptionDto.setQuestionId(question.getId());
                List<QuestionOptionDto> questionOptionDtoList = myQuestionMapper.findQuestionOptionList(questionOptionDto);
                question.setOptionDtoList(questionOptionDtoList);
            }
        }

        //返回结果
        questionDto.setTotal(pageInfo.getTotal());
        questionDto.setRows(questionList);
    }

    /**
     * id查询
     */
    public QuestionDto getById(String id) {
        return questionMapper.getById(id);
    }

    /**
     * id查询
     */
    public QuestionDto getByIdExtend(String id) {
        QuestionDto questionDto = questionMapper.getById(id);
        if (ObjectUtils.isNotEmpty(questionDto)) {
            //查询选项
            List<QuestionOptionDto> questionOptions = questionOptionMapper.listByQstId(id);
            if (ObjectUtils.isNotEmpty(questionOptions)) {
                questionDto.setOptionDtoList(questionOptions);
                Collections.sort(questionDto.getOptionDtoList(), (o1, o2) -> {
                    int index1 = MyUtils.OPTION_INDEX.get(o1.getOption());
                    int index2 = MyUtils.OPTION_INDEX.get(o2.getOption());
                    //升序
                    if (index1 < index2) {
                        return -1;
                    } else if (index1 > index2) {
                        return 1;
                    }
                    return 0;
                });
            }
            //处理试题分类
            if (ObjectUtils.isNotEmpty(questionDto.getClassifyDtoList())) {
                String[] classifyIds=new String[questionDto.getClassifyDtoList().size()];
                for (int i = 0; i < questionDto.getClassifyDtoList().size(); i++) {
                    ClassifyDto classifyDto = questionDto.getClassifyDtoList().get(i);
                    classifyIds[i]=classifyDto.getId();
                }
                questionDto.setClassifyIds(classifyIds);
            }
            return questionDto;
        }
        return null;
    }

    /**
     * 保存
     */
    @Transactional
    public void save(QuestionDto questionDto) {
        if (StringUtils.isNotBlank(questionDto.getId())) {
            //修改试题
            questionDto.setUpdateTime(new Date());
            questionDto.setLastUpdateUserId(questionDto.getCurrentUserId());
            questionMapper.update(questionDto);
            //主观题直接返回，不用添加选项
            if (questionDto.getType().equals(QuestionTypeEnum.ZHUGUAN.getCode())){
                return;
            }
            String[] ids = {questionDto.getId()};
            //删除选项
            questionOptionMapper.deleteByQstId(ids);
            //删除试题分类
            questionClassifyMapper.deleteByQstId(ids);
        } else {
            //添加试题
            questionDto.setId(UuidUtil.getShortUuid());
            questionDto.setCreateTime(new Date());
            questionDto.setCreateUserId(questionDto.getCurrentUserId());
            questionMapper.insert(questionDto);
            //主观题直接返回，不用添加选项
            if (questionDto.getType().equals(QuestionTypeEnum.ZHUGUAN.getCode())){
                return;
            }
        }
        //更新选项
        if (ObjectUtils.isNotEmpty(questionDto.getOptionDtoList())) {
            //添加选项
            List<QuestionOptionDto> list = new ArrayList<>();
            Date current=new Date();
            for (QuestionOptionDto optionDto : questionDto.getOptionDtoList()) {
                optionDto.setId(UuidUtil.getShortUuid());
                optionDto.setQuestionId(questionDto.getId());
                optionDto.setCreateTime(current);
                list.add(optionDto);
            }
            questionOptionMapper.insertBatch(list);
        }
        //更新试题分类
        if (ObjectUtils.isNotEmpty(questionDto.getClassifyIds())) {
            List<QuestionClassify> list = new ArrayList<>();
            Date current=new Date();
            for (String classifyId : questionDto.getClassifyIds()) {
                QuestionClassify questionClassify = new QuestionClassify();
                questionClassify.setId(UuidUtil.getShortUuid());
                questionClassify.setQuestionId(questionDto.getId());
                questionClassify.setClassifyId(classifyId);
                questionClassify.setCreateTime(current);
                list.add(questionClassify);
            }
            questionClassifyMapper.insertBatch(list);
        }
    }

    /**
     * 删除
     */
    @Transactional
    public void delete(String[] ids) {
        //删除试题
        questionMapper.delete(ids);
        //删除试题选项
        questionOptionMapper.deleteByQstId(ids);
        //删除试题分类
        questionClassifyMapper.deleteByQstId(ids);
    }

    public List<QuestionDto> getQuestionByClassifyId(String classifyId, Integer qstNum, String qstType) {
        return questionMapper.getQuestionByClassifyId(classifyId,qstNum,qstType);
    }

    public List<QuestionDto> getQuestionByLabelId(String labelId, Integer qstNum) {
        return questionMapper.getQuestionByLabelId(labelId,qstNum);
    }
}
